package com.mars.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.mars.common.enums.BusinessType;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.SysCommunityHouse;
import com.mars.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.mars.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.service.ISysCommunityHouseService;
import com.mars.module.admin.request.SysCommunityHouseRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 社区房屋控制层
 *
 * @author mars
 * @date 2024-04-15
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "社区房屋接口管理",tags = "社区房屋接口管理")
@RequestMapping("/admin/sysCommunityHouse" )
public class SysCommunityHouseController {

    private final ISysCommunityHouseService iSysCommunityHouseService;

    /**
     * 分页查询社区房屋列表
     */
    @ApiOperation(value = "分页查询社区房屋列表")
    @PostMapping("/pageList")
    public R<PageInfo<SysCommunityHouse>> pageList(@RequestBody SysCommunityHouseRequest sysCommunityHouseRequest) {
        return R.success(iSysCommunityHouseService.pageList(sysCommunityHouseRequest));
    }

    /**
     * 获取社区房屋详细信息
     */
    @ApiOperation(value = "获取社区房屋详细信息")
    @GetMapping(value = "/query/{id}")
    public R<SysCommunityHouse> detail(@PathVariable("id") Long id) {
        return R.success(iSysCommunityHouseService.getById(id));
    }

    /**
     * 新增社区房屋
     */
    @Log(title = "新增社区房屋", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增社区房屋")
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysCommunityHouseRequest sysCommunityHouseRequest) {
        iSysCommunityHouseService.add(sysCommunityHouseRequest);
        return R.success();
    }

    /**
     * 修改社区房屋
     */
    @Log(title = "修改社区房屋", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改社区房屋")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody SysCommunityHouseRequest sysCommunityHouseRequest) {
        iSysCommunityHouseService.update(sysCommunityHouseRequest);
        return R.success();
    }

    /**
     * 删除社区房屋
     */
    @Log(title = "删除社区房屋", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除社区房屋")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iSysCommunityHouseService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出社区房屋
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody SysCommunityHouseRequest sysCommunityHouseRequest) throws IOException {
        List<SysCommunityHouse> list = iSysCommunityHouseService.list(sysCommunityHouseRequest);
        ExcelUtils.exportExcel(list, SysCommunityHouse.class, "社区房屋信息", response);
    }
}
